#include "respondcodec.h"

RespondCodec::RespondCodec() : Codec()
{

}

RespondCodec::RespondCodec(RespondInfo *info) : Codec()
{
    initMessage(info);
}

RespondCodec::RespondCodec(QString msg) : Codec()
{
    initMessage(msg);
}

RespondCodec::~RespondCodec()
{

}

void RespondCodec::initMessage(RespondInfo *info)
{
    m_msg.set_status(info->status);
    m_msg.set_data(info->data.toStdString());
}

void RespondCodec::initMessage(QString msg)
{
    // base64 解码
    QByteArray array = QByteArray::fromBase64(msg.toUtf8());
    // 转换为string 类型
    m_encMsg = string(array.data(), array.size());
}

QString RespondCodec::encodeMsg()
{
    string output;
    m_msg.SerializeToString(&output);
    QByteArray data = QByteArray(output.data(), output.size()).toBase64();
    return data;
}

void RespondCodec::decodeMsg(void *output)
{
    m_msg.ParseFromString(m_encMsg);
    // 读出对象中的数据
    m_info.data = QString::fromStdString(m_msg.data());
    m_info.status = m_msg.status();
    // 数据传出, 赋值给传出参数
    *(RespondInfo*)output = m_info;
}
